package day04;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class A01_应用域 extends HttpServlet {
    /**
     * 1.概述
     *  Servlet规范中,一共有4大域对象
     *  域对象作用: 共享数据,不同的域对象共享数据的范围不同
     *      会话域对象(HttpSession)
     *      请求域
     *      页面域
     *      应用域(ServletContext)
     *
     *  ServletContext(application)
     *      应用域:
     *          共享数据范围: 整个应用
     *          应用的初始化参数的配置  以后再说
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //中文乱码问题
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //获取ServletContext对象  public ServletContext getServletContext();
        ServletContext context = req.getServletContext();
        //向应用域中存储数据
        context.setAttribute("李云龙","你他娘的意大利炮呢");
        //获取应用域中的数据
        Object o = context.getAttribute("李云龙");
        resp.getWriter().write(o.toString());

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
    }
}
